<?php

class CatalogueController extends AdminController {

    public $parentUrl;

    public function init() {
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }

    public function actionIndex() {
        try {
            $model = Catalogue::model()->findAll();
            $this->render('index', array(
                'data' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionView($id) {
        try {
            $this->render('view', array(
                'model' => $this->loadModel($id),
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionCreate() {
        //try {
            $model = new Catalogue();
            if (isset($_POST['Catalogue'])) {
                $model->attributes = $_POST['Catalogue'];
                $object_file = CUploadedFile::getInstance($model, 'file_name');
                if ($object_file !== null) {
                    $model['file_ext'] = $object_file->type;
                    $model['old_name'] = $object_file->name;
                    $model['url'] = Yii::app()->basePath . "/../data/catalogue/";
                    $ran = rand(0, 999999999);
                    $fileName = trim(date('Y-m-d H-i-s') . $ran) . '.' . $object_file->extensionName;
                    $model['file_name'] = $fileName;
                    $object_file->saveAs(Yii::app()->basePath . "/../data/catalogue/$fileName");
                }
                $model->validate();
                if (!$model->hasErrors()) {
                    $model->name = $_POST['Catalogue']['name'];
                    if ($model->save()) {                        
                        $this->redirect(Yii::app()->createAbsoluteUrl('systems/catalogue/index'));
                    }
                }
            }
            $this->render('create', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
//        } catch (Exception $e) {
//            Yii::log("Invalid request. Please do not repeat this request again.");
//            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
//        }
    }

    public function actionUpdate($id) {
        try {
            $model = $this->loadModel($id);
            if (isset($_POST['Catalogue'])) {
                $model->attributes = $_POST['Catalogue'];
                $model->validate();
                if (!$model->hasErrors()) {
                    if ($model->save()) {
                        $this->redirect(array('view', 'id' => $model->id));
                    }
                }
            }
            $this->render('update', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionDelete($id) {
        try {
            if ($model = $this->loadModel($id)) {
                if ($model->delete()) {
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/catalogue/index'));
                }
            }
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
        } catch (Exception $e) {
            Yii::log("Exception " . print_r($e, true), 'error');
            throw new CHttpException("Exception " . print_r($e, true));
        }
    }

    public function loadModel($id) {
        try {
            $model = Catalogue::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function saveCatalogue($model) {
        $model->name = isset($_POST['Catalogue']['name']) ? $_POST['Catalogue']['name'] : '';
        $model->file_name = isset($_POST['Catalogue']['file_name']) ? $_POST['Catalogue']['file_name'] : '';
        $model->save();
    }

}

?>
